<template>
  <card-box
    title="拖拽列表"
    color="linear-gradient(60deg,#26c6da,#00acc1)"
    icon="icon-template_template"
    minHeight="350px"
  >
    <div slot="card-content" class="todolist">
      <div class="panel-body">
        <div class="checkbox">
          <label>
            <input type="checkbox" v-model="editable">是否可拖拽
          </label>
        </div>
        <!-- <button type="button" class="btn btn-default" @click="orderList">
          Sort by original order
        </button>-->
      </div>

      <div>
        <draggable
          class="list-group"
          element="ul"
          v-model="list"
          :options="dragOptions"
          :move="onMove"
          @start="isDragging = true"
          @end="isDragging = false"
        >
          <transition-group type="transition" :name="'flip-list'">
            <li class="list-group-item" v-for="element in list" :key="element.order">
              <i
                :class="
                element.fixed ? 'fa fa-anchor' : 'glyphicon glyphicon-pushpin'
              "
                @click="element.fixed = !element.fixed"
                aria-hidden="true"
              ></i>
              {{ element.name }}
              <span class="badge">{{ element.order }}</span>
            </li>
          </transition-group>
        </draggable>
        <!-- <div class="col-md-3" style="float:left">
        <pre>{{ listString }}</pre>
        </div>-->
      </div>
    </div>
  </card-box>
</template>

<script>
import draggable from "vuedraggable";
import CardBox from "@/components/CardBox"
const message = [
  "vue.draggable",
  "draggable",
  "component",
  "for",
  "vue.js 2.0",
  "based",
  "on",
  "Sortablejs"
];
export default {
  name: "drag",
  components: {
    CardBox,
    draggable
  },
  data () {
    return {
      list: message.map((name, index) => {
        return { name, order: index + 1, fixed: false };
      }),
      editable: true,
      isDragging: false,
      delayedDragging: false
    };
  },
  methods: {
    orderList () {
      this.list = this.list.sort((one, two) => {
        return one.order - two.order;
      });
    },
    onMove ({ relatedContext, draggedContext }) {
      const relatedElement = relatedContext.element;
      const draggedElement = draggedContext.element;
      return (
        (!relatedElement || !relatedElement.fixed) && !draggedElement.fixed
      );
    }
  },
  computed: {
    dragOptions () {
      return {
        animation: 0,
        group: "description",
        disabled: !this.editable,
        ghostClass: "ghost"
      };
    },
    listString () {
      return JSON.stringify(this.list, null, 2);
    }
  },
  watch: {
    isDragging (newValue) {
      if (newValue) {
        this.delayedDragging = true;
        return;
      }
      this.$nextTick(() => {
        this.delayedDragging = false;
      });
    }
  }
};
</script>

<style>
.flip-list-move {
  transition: transform 0.5s;
}
.no-move {
  transition: transform 0s;
}
.ghost {
  opacity: 0.5;
  background: #c8ebfb;
}
.list-group {
  width: 340px;
  float: left;
  padding: 20px;
  background: #f2f2f2;
  overflow: hidden;
  min-height: 300px;
}
.list-group-item:first-child {
  border-top-left-radius: 4px;
  border-top-right-radius: 4px;
}
.list-group-item {
  width: 100px;
  height: 100px;
  float: left;
  cursor: move;
  position: relative;
  display: block;
  padding: 10px 15px;
  margin-bottom: -1px;
  background-color: #fff;
  border: 1px solid #ddd;
}
.list-group-item i {
  cursor: pointer;
}
</style>
